<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Setting extends Admin_Controller
{
  private $module = 'setting';
  private $table = 'settings';

  function __construct()
  {
    parent::__construct();
    if ($this->uri->segment(3) != 'login') {
      if (!$this->session->userdata('userInfo')) {
        header('Location: ' . base_url(ADMIN_PATCH . '/users/login'));
        return false;
      }
    }
    $this->load->model($this->module . '_model', 'model');
  }

//  Frontend Function
  public function index()
  {
    if (!empty($_POST)) {
      foreach ($this->input->post('contentAdmincp') as $k => $v) {
        $chk_slug = $this->model->checkSlug($k);
        if ($chk_slug) {
          $data = array(
            'content' => $v,
            'modified' => date('Y-m-d H:i:s')
          );
          $this->db->where('id', $chk_slug[0]->id);
          $this->db->update('admin_nqt_settings', $data);
        } else {
          $data = array(
            'slug' => $k,
            'content' => $v,
            'modified' => date('Y-m-d H:i:s')
          );
          $this->db->insert('admin_nqt_settings', $data);
        }
      }
      print 'success-setting';
      exit;
    } else {
      $this->breadcrumbs->push('Cấu hình', ADMIN_PATCH . '/setting');
      $this->breadcrumbs->push('Cấu hình chung', '/');
//      $this->breadcrumbs->unshift('Home', '/');
      $data = array(
        'setting' => $this->model->getSetting(),
        'module' => $this->module,
        'module_name' => 'Cấu hình',
        'breadcrumbs' => $this->breadcrumbs->show()
      );
      $this->twiggy->set($data, NULL, TRUE);
      $this->twiggy->template('setting')->display();
    }

  }
//  Private Function

}
